﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ECare.BUS;
using ECare.BUS.Data;
using ECare.Client.frms;

namespace ECare.Win.controls
{
    public partial class ucOut : BaseModule
    {
        public ucOut()
        {
            InitializeComponent();

            this.Load += ucOut_Load;

            this.windowsUIButtonPanel1.ButtonClick += windowsUIButtonPanel1_ButtonClick;
        }

        void windowsUIButtonPanel1_ButtonClick(object sender, DevExpress.XtraBars.Docking2010.ButtonEventArgs e)
        {

            if (e.Button.Properties.Caption == "New")
            {
                var frm = new frmNewOutInfo();
                frm.entity = null;
                frm.Saved += frm_Saved;
                frm.ShowDialog();
            }
            else if (e.Button.Properties.Caption == "Edit")
            {
                if (this.grd.SelectedRows.Count == 0)
                    return;

                var entitye = this.grd.SelectedRows[0].DataBoundItem as PlanOutInfo;
                var frm = new frmNewOutInfo();
                frm.entity = entitye;
                frm.ShowDialog();
            }
            else if (e.Button.Properties.Caption == "Out")
            {
                if (this.grd.SelectedRows.Count == 0)
                    return;
                var entitye = this.grd.SelectedRows[0].DataBoundItem as PlanOutInfo;



                if (entitye.plan_isout)
                {
                    MessageBox.Show("当前出库单已经出库.","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    return;
                }


                if (entitye != null && entitye.plan_out_check_status != CheckStatus.Pending)
                {
                    MessageBox.Show("出库单已经被审核.", "信息提示");
                    return;
                }var frm = new frmOutFormInfo { entity = this.grd.SelectedRows[0].DataBoundItem as PlanOutInfo };
                frm.ShowDialog();
                this.grd.Refresh();

            }
            else if(e.Button.Properties.Caption=="Cancel")
            {

            }
            else
            {
                
            }


        }

        private void CancelOut()
        {

        }



        void frm_Saved(object entity)
        {
            if (source == null)
                source = new BindingList<PlanOutInfo>();
            var role = entity as PlanOutInfo;
            var findresult = source.FirstOrDefault(a => a.plan_out_id == role.plan_out_id);
            if (findresult == null)
                this.source.Add(role);
        }

        public BindingList<PlanOutInfo> source { get; set; }
        void ucOut_Load(object sender, EventArgs e)
        {
            source = new BindingList<PlanOutInfo>(PlanHelper.GetPlanOuts());
            this.grd.DataSource = source;
        }
    }
}
